[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
There are three ways to investigate a problem in an Emacs Lisp program, depending on what you are doing with the program when the problem appears.
1.1 The Lisp Debugger | How the Emacs Lisp debugger is implemented. | |
1.2 Debugging Invalid Lisp Syntax | How to find syntax errors. | |
1.3 Debugging Problems in Compilation | How to find errors that show up in byte compilation. | |
1.4 Edebug | A source-level Emacs Lisp debugger. |
Another useful debugging tool is a dribble file. When a dribble file is open, Emacs copies all keyboard input characters to that file. Afterward, you can examine the file to find out what input was used. @xref{Terminal Input}.
For debugging problems in terminal descriptions, the
open-termscript
function can be useful. @xref{Terminal Output}.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The Lisp debugger provides you with the ability to suspend evaluation of a form. While evaluation is suspended (a state that is commonly known as a break), you may examine the run time stack, examine the values of local or global variables, or change those values. Since a break is a recursive edit, all the usual editing facilities of Emacs are available; you can even run programs that will enter the debugger recursively. @xref{Recursive Editing}.
1.1.1 Entering the Debugger on an Error | Entering the debugger when an error happens. | |
1.1.2 Debugging Infinite Loops | Stopping and debugging a program that doesn’t exit. | |
1.1.3 Entering the Debugger on a Function Call | Entering it when a certain function is called. | |
1.1.4 Explicit Entry to the Debugger | Entering it at a certain point in the program. | |
1.1.5 Using the Debugger | What the debugger does; what you see while in it. | |
1.1.6 Debugger Commands | Commands used while in the debugger. | |
1.1.7 Invoking the Debugger | How to call the function debug .
| |
1.1.8 Internals of the Debugger | Subroutines of the debugger, and global variables. |
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The most important time to enter the debugger is when a Lisp error happens. This allows you to investigate the immediate causes of the error.
However, entry to the debugger is not a normal consequence of an
error. Many commands frequently get Lisp errors when invoked in
inappropriate contexts (such as C-f at the end of the buffer) and
during ordinary editing it would be very unpleasant to enter the
debugger each time this happens. If you want errors to enter the
debugger, set the variable debug-on-error
to non-nil
.
This variable determines whether the debugger is called when a error is
signaled and not handled. If debug-on-error
is t
, all
errors call the debugger. If it is nil
, none call the debugger.
The value can also be a list of error conditions that should call the
debugger. For example, if you set it to the list
(void-variable)
, then only errors about a variable that has no
value invoke the debugger.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
When a program loops infinitely and fails to return, your first problem is to stop the loop. On most operating systems, you can do this with C-g, which causes quit.
Ordinary quitting gives no information about why the program was
looping. To get more information, you can set the variable
debug-on-quit
to non-nil
. Quitting with C-g is not
considered an error, and debug-on-error
has no effect on the
handling of C-g. Contrariwise, debug-on-quit
has no effect
on errors.
Once you have the debugger running in the middle of the infinite loop, you can proceed from the debugger using the stepping commands. If you step through the entire loop, you will probably get enough information to solve the problem.
This variable determines whether the debugger is called when quit
is signaled and not handled. If debug-on-quit
is non-nil
,
then the debugger is called whenever you quit (that is, type C-g).
If debug-on-quit
is nil
, then the debugger is not called
when you quit. @xref{Quitting}.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
To investigate a problem that happens in the middle of a program, one useful technique is to cause the debugger to be entered when a certain function is called. You can do this to the function in which the problem occurs, and then step through the function, or you can do this to a function called shortly before the problem, step quickly over the call to that function, and then step through its caller.
This function requests function-name to invoke the debugger each time
it is called. It works by inserting the form (debug 'debug)
into
the function definition as the first form.
Any function defined as Lisp code may be set to break on entry, regardless of whether it is interpreted code or compiled code. Even functions that are commands may be debugged—they will enter the debugger when called inside a function, or when called interactively (after the reading of the arguments). Primitive functions (i.e., those written in C) may not be debugged.
When debug-on-entry
is called interactively, it prompts
for function-name in the minibuffer.
Caveat: if debug-on-entry
is called more than once on the same
function, the second call does nothing. If you redefine a function
after using debug-on-entry
on it, the code to enter the debugger
is lost.
debug-on-entry
returns function-name.
(defun fact (n) (if (zerop n) 1 (* n (fact (1- n))))) ⇒ fact
(debug-on-entry 'fact) ⇒ fact
(fact 3) ⇒ 6
------ Buffer: *Backtrace* ------ Entering: * fact(3) eval-region(4870 4878 t) byte-code("...") eval-last-sexp(nil) (let ...) eval-insert-last-sexp(nil) * call-interactively(eval-insert-last-sexp) ------ Buffer: *Backtrace* ------
(symbol-function 'fact) ⇒ (lambda (n) (debug (quote debug)) (if (zerop n) 1 (* n (fact (1- n)))))
This function undoes the effect of debug-on-entry
on
function-name. When called interactively, it prompts for
function-name in the minibuffer.
If cancel-debug-on-entry
is called more than once on the same
function, the second call does nothing. cancel-debug-on-entry
returns function-name.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
You can cause the debugger to be called at a certain point in your
program by writing the expression (debug)
at that point. To do
this, visit the source file, insert the text ‘(debug)’ at the
proper place, and type C-M-x. Be sure to undo this insertion
before you save the file!
The place where you insert ‘(debug)’ must be a place where an
additional form can be evaluated and its value ignored. (If the value
isn’t ignored, it will alter the execution of the program!) Usually
this means inside a progn
or an implicit progn
(@pxref{Sequencing}).
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
When the debugger is entered, it displays the previously selected buffer in one window and a buffer named ‘*Backtrace*’ in another window. The backtrace buffer contains one line for each level of Lisp function execution currently going on. At the beginning of this buffer is a message describing the reason that the debugger was invoked (such as the error message and associated data, if it was invoked due to an error).
The backtrace buffer is read-only and uses a special major mode, Debugger mode, in which letters are defined as debugger commands. The usual Emacs editing commands are available; thus, you can switch windows to examine the buffer that was being edited at the time of the error, switch buffers, visit files, or do any other sort of editing. However, the debugger is a recursive editing level (@pxref{Recursive Editing}) and it is wise to go back to the backtrace buffer and exit the debugger (with the q command) when you are finished with it. Exiting the debugger gets out of the recursive edit and kills the backtrace buffer.
The contents of the backtrace buffer show you the functions that are executing and the arguments that were given to them. It also allows you to specify a stack frame by moving point to the line describing that frame. (A stack frame is the place where the Lisp interpreter records information about a particular invocation of a function. The frame whose line point is on is considered the current frame.) Some of the debugger commands operate on the current frame.
The debugger itself should always be run byte-compiled, since it makes assumptions about how many stack frames are used for the debugger itself. These assumptions are false if the debugger is running interpreted.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Inside the debugger (in Debugger mode), these special commands are available in addition to the usual cursor motion commands. (Keep in mind that all the usual facilities of Emacs, such as switching windows or buffers, are still available.)
The most important use of debugger commands is for stepping through code, so that you can see how control flows. The debugger can step through the control structures of an interpreted function, but cannot do so in a byte-compiled function. If you would like to step through a byte-compiled function, replace it with an interpreted definition of the same function. (To do this, visit the source file for the function and type C-M-x on its definition.)
Exit the debugger and continue execution. When continuing is possible, it resumes execution of the program as if the debugger had never been entered (aside from the effect of any variables or data structures you may have changed while inside the debugger).
Continuing is possible after entry to the debugger due to function entry or exit, explicit invocation, quitting or certain errors. Most errors cannot be continued; trying to continue an unsuitable error causes the same error to occur again.
Continue execution, but enter the debugger the next time any Lisp function is called. This allows you to step through the subexpressions of an expression, seeing what values the subexpressions compute, and what else they do.
The stack frame made for the function call which enters the debugger in this way will be flagged automatically so that the debugger will be called again when the frame is exited. You can use the u command to cancel this flag.
Flag the current frame so that the debugger will be entered when the frame is exited. Frames flagged in this way are marked with stars in the backtrace buffer.
Don’t enter the debugger when the current frame is exited. This cancels a b command on that frame.
Read a Lisp expression in the minibuffer, evaluate it, and print the value in the echo area. This is the same as the command M-<ESC>, except that e is not normally disabled like M-<ESC>.
Terminate the program being debugged; return to top-level Emacs command execution.
If the debugger was entered due to a C-g but you really want to quit, and not debug, use the q command.
Return a value from the debugger. The value is computed by reading an expression with the minibuffer and evaluating it.
The r command makes a difference when the debugger was invoked due to exit from a Lisp call frame (as requested with b); then the value specified in the r command is used as the value of that frame.
You can’t use r when the debugger was entered due to an error.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Here we describe fully the function used to invoke the debugger.
This function enters the debugger. It switches buffers to a buffer named ‘*Backtrace*’ (or ‘*Backtrace*<2>’ if it is the second recursive entry to the debugger, etc.), and fills it with information about the stack of Lisp function calls. It then enters a recursive edit, leaving that buffer in Debugger mode and displayed in the selected window.
Debugger mode provides a c command which operates by exiting the
recursive edit, switching back to the previous buffer, and returning to
whatever called debug
. The r command also returns from
debug
. These are the only ways the function debug
can
return to its caller.
If the first of the debugger-args passed to debug
is
nil
(or if it is not one of the following special values), then
the rest of the arguments to debug
are printed at the top of the
‘*Backtrace*’ buffer. This mechanism is used to display a message
to the user.
However, if the first argument passed to debug
is one of the
following special values, then it has special significance. Normally,
these values are passed to debug
only by the internals of Emacs
and the debugger, and not by programmers calling debug
.
The special values are:
lambda
When the first argument is lambda
, the debugger displays
‘Entering:’ as a line of text at the top of the buffer. This means
that a function is being entered when debug-on-next-call
is
non-nil
.
debug
When the first argument is debug
, the debugger displays
‘Entering:’ just as in the lambda
case. However,
debug
as the argument indicates that the reason for entering the
debugger is that a function set to debug on entry is being entered.
In addition, debug
as the first argument directs the debugger
to mark the function that called debug
so that it will invoke the
debugger when exited. (When lambda
is the first argument, the
debugger does not do this, because it has already been done by the
interpreter.)
t
When the first argument is t
, the debugger displays the following
as the top line in the buffer:
Beginning evaluation of function call form:
This indicates that it was entered due to the evaluation of a list form
at a time when debug-on-next-call
is non-nil
.
exit
When the first argument is exit
, it indicates the exit of a
stack frame previously marked to invoke the debugger on exit. The
second argument given to debug
in this case is the value being
returned from the frame. The debugger displays ‘Return value:’ on
the top line of the buffer, followed by the value being returned.
error
When the first argument is error
, the debugger indicates that
it is being entered because an error or quit
was signaled and not
handled, by displaying ‘Signaling:’ followed by the error signaled
and any arguments to signal
. For example,
(let ((debug-on-error t)) (/ 1 0))
------ Buffer: *Backtrace* ------ Signaling: (arith-error) /(1 0) ... ------ Buffer: *Backtrace* ------
If an error was signaled, presumably the variable
debug-on-error
is non-nil
. If quit
was signaled,
then presumably the variable debug-on-quit
is non-nil
.
nil
Use nil
as the first of the debugger-args when you want
to enter the debugger explicitly. The rest of the debugger-args
are printed on the top line of the buffer. You can use this feature to
display messages—for example, to remind yourself of the conditions
under which debug
is called.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This section describes functions and variables used internally by the debugger.
The value of this variable is the function to call to invoke the
debugger. Its value must be a function of any number of arguments (or,
more typically, the name of a function). Presumably this function will
enter some kind of debugger. The default value of the variable is
debug
.
The first argument that Lisp hands to the function indicates why it
was called. The convention for arguments is detailed in the description
of debug
.
This function prints a trace of Lisp function calls currently active.
This is the function used by debug
to fill up the
‘*Backtrace*’ buffer. It is written in C, since it must have access
to the stack to determine which function calls are active. The return
value is always nil
.
In the following example, backtrace
is called explicitly in a
Lisp expression. When the expression is evaluated, the backtrace is
printed to the stream standard-output
: in this case, to the
buffer ‘backtrace-output’. Each line of the backtrace represents
one function call. If the arguments of the function call are all known,
they are displayed; if they are being computed, that fact is stated.
The arguments of special forms are elided.
(with-output-to-temp-buffer "backtrace-output" (let ((var 1)) (save-excursion (setq var (eval '(progn (1+ var) (list 'testing (backtrace)))))))) ⇒ nil
----------- Buffer: backtrace-output ------------ backtrace() (list ...computing arguments...) (progn ...) eval((progn (1+ var) (list (quote testing) (backtrace)))) (setq ...) (save-excursion ...) (let ...) (with-output-to-temp-buffer ...) eval-region(1973 2142 #<buffer *scratch*>) byte-code("... for eval-print-last-sexp ...") eval-print-last-sexp(nil) * call-interactively(eval-print-last-sexp) ----------- Buffer: backtrace-output ------------
The character ‘*’ indicates a frame whose debug-on-exit flag is set.
This variable determines whether the debugger is called before the
next eval
, apply
or funcall
. It is automatically
reset to nil
when the debugger is entered.
The d command in the debugger works by setting this variable.
This function sets the debug-on-exit flag of the eval frame
level levels down to flag. If flag is non-nil
,
this will cause the debugger to be entered when that frame exits.
Even a nonlocal exit through that frame will enter the debugger.
The debug-on-exit flag is an entry in the stack frame of a function call. This flag is examined on every exit from a function.
Normally, this function is only called by the debugger.
This variable records the debugging status of current interactive
command. Each time a command is called interactively, this variable is
bound to nil
. The debugger can set this variable to leave
information for future debugger invocations during the same command.
The advantage of using this variable rather that defining another global variable is that the data will never carry over to a later other command invocation.
The function backtrace-frame
is intended for use in Lisp
debuggers. It returns information about what computation is happening
in the eval frame level levels down.
If that frame has not evaluated the arguments yet (or is a special
form), the value is (nil function arg-forms…)
.
If that frame has evaluated its arguments and called its function
already, the value is (t function
arg-values…)
.
In the return value, function is whatever was supplied as CAR
of evaluated list, or a lambda
expression in the case of a macro
call. If the function has a &rest
argument, that is represented
as the tail of the list arg-values.
If the argument is out of range, backtrace-frame
returns
nil
.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The Lisp reader reports invalid syntax, but cannot say where the real problem is. For example, the error “End of file during parsing” in evaluating an expression indicates an excess of open parentheses (or square brackets). The reader detects this imbalance at the end of the file, but it cannot figure out where the close parenthesis should have been. Likewise, “Invalid read syntax: ")"” indicates an excess close parenthesis or missing open parenthesis, but not where the missing parenthesis belongs. How, then, to find what to change?
If the problem is not simply an imbalance of parentheses, a useful technique is to try C-M-e at the beginning of each defun, and see if it goes to the place where that defun appears to end. If it does not, there is a problem in that defun.
However, unmatched parentheses are the most common syntax errors in Lisp, and we can give further advice for those cases.
1.2.1 Excess Open Parentheses | How to find a spurious open paren or missing close. | |
1.2.2 Excess Close Parentheses | How to find a spurious close paren or missing open. |
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The first step is to find the defun that is unbalanced. If there is
an excess open parenthesis, the way to do this is to insert a
close parenthesis at the end of the file and type C-M-b
(backward-sexp
). This will move you to the beginning of the
defun that is unbalanced. (Then type C-<SPC> C-_ C-u
C-<SPC> to set the mark there, undo the insertion of the
close parenthesis, and finally return to the mark.)
The next step is to determine precisely what is wrong. There is no way to be sure of this except to study the program, but often the existing indentation is a clue to where the parentheses should have been. The easiest way to use this clue is to reindent with C-M-q and see what moves.
Before you do this, make sure the defun has enough close parentheses. Otherwise, C-M-q will get an error, or will reindent all the rest of the file until the end. So move to the end of the defun and insert a close parenthesis there. Don’t use C-M-e to move there, since that too will fail to work until the defun is balanced.
Then go to the beginning of the defun and type C-M-q. Usually all the lines from a certain point to the end of the function will shift to the right. There is probably a missing close parenthesis, or a superfluous open parenthesis, near that point. (However, don’t assume this is true; study the code to make sure.) Once you have found the discrepancy, undo the C-M-q, since the old indentation is probably appropriate to the intended parentheses.
After you think you have fixed the problem, use C-M-q again. It should not change anything, if the problem is really fixed.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
To deal with an excess close parenthesis, first insert an open parenthesis at the beginning of the file and type C-M-f to find the end of the unbalanced defun. (Then type C-<SPC> C-_ C-u C-<SPC> to set the mark there, undo the insertion of the open parenthesis, and finally return to the mark.)
Then find the actual matching close parenthesis by typing C-M-f at the beginning of the defun. This will leave you somewhere short of the place where the defun ought to end. It is possible that you will find a spurious close parenthesis in that vicinity.
If you don’t see a problem at that point, the next thing to do is to type C-M-q at the beginning of the defun. A range of lines will probably shift left; if so, the missing open parenthesis or spurious close parenthesis is probably near the first of those lines. (However, don’t assume this is true; study the code to make sure.) Once you have found the discrepancy, undo the C-M-q, since the old indentation is probably appropriate to the intended parentheses.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
When an error happens during byte compilation, it is normally due to invalid syntax in the program you are compiling. The compiler prints a suitable error message in the ‘*Compile-Log*’ buffer, and then stops. The message may state a function name in which the error was found, or it may not. Regardless, here is how to find out where in the file the error occurred.
What you should do is switch to the buffer ‘ *Compiler Input*’. (Note that the buffer name starts with a space, so it does not show up in M-x list-buffers.) This buffer contains the program being compiled, and point shows how far the byte compiler was able to read.
If the error was due to invalid Lisp syntax, point shows exactly where the invalid syntax was detected. The cause of the error is not necessarily near by! Use the techniques in the previous section to find the error.
If the error was detected while compiling a form that had been read successfully, then point is located at the end of the form. In this case, it can’t localize the error precisely, but can still show you which function to check.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Edebug is a source-level debugger for Emacs Lisp programs that provides the following features:
The first three sections of this chapter should tell you enough about Edebug to enable you to use it.
1.4.1 Using Edebug | Introduction to use of Edebug. | |
1.4.2 Preparing Functions for Edebug | You must prepare a function or macro definition in order to debug it with Edebug. | |
1.4.3 Edebug Modes | Execution modes, stopping more or less often. | |
1.4.4 Stepping | Commands to step to a specified place. | |
1.4.6 Breakpoints | Setting breakpoints to make the program stop. | |
1.4.7 Views | Viewing the outside buffer and window status. | |
1.4.8 Evaluation | Evaluating expressions within Edebug. | |
1.4.9 Evaluation List Buffer | Expressions whose values are displayed each time you enter Edebug. | |
1.4.5 Miscellaneous | ||
1.4.10 Printing | Printing circular structure in Edebug. | |
1.4.11 The Outside Context | Data that Edebug saves and restores. | |
1.4.12 Macro Calls | Explaining how to handle macro calls. | |
1.4.13 Edebug Options | Option variables for customizing Edebug. |
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
To debug a Lisp program with Edebug, you must first prepare the Lisp functions that you want to debug. See section Preparing Functions for Edebug.
Once a function is prepared, any call to the function activates Edebug. This involves entering a recursive edit which is a level of Edebug activation.
Activating Edebug may stop execution and let you step through the function, or it may continue execution while checking for debugging commands, depending on the selected Edebug execution mode. See section Edebug Modes.
Within Edebug, you normally view an Emacs buffer showing the source of the Lisp function you are debugging. We call this the Edebug buffer—but note that it is not always the same buffer, and it is not reserved for Edebug use.
An arrow at the left margin indicates the line where the function is executing. Point initially shows where within the line the function is executing, but this ceases to be true if you move point yourself.
If you prepare the definition of fac
(shown below) for Edebug and
then execute (fac 3)
, here is what you normally see. Point is at
the open-parenthesis before if
.
(defun fac (n) =>∗(if (< 0 n) (* n (fac (1- n))) 1))
The places within a function where Edebug can stop execution are called
stop points. These occur both before and after each subexpression
that is a list, and also after each variable reference. Stop points
before variables are optional, under the control of the value of
edebug-stop-before-symbols
. Here we show with periods the stop
points normally found in the function fac
:
(defun fac (n) .(if .(< 0 n.). .(* n. .(fac (1- n.).).). 1).)
While a buffer is the Edebug buffer, the special commands of Edebug are available in it, instead of many usual editing commands. Type ? to display a list of Edebug commands. In particular, you can exit the innermost Edebug activation level with C-], and you can return all the way to top level with q.
For example, you can type the Edebug command <SPC> to execute until
the next stop point. If you type <SPC> once after entry to
fac
, here is the state that you get:
(defun fac (n) =>(if ∗(< 0 n) (* n (fac (1- n))) 1))
When Edebug stops execution after an expression, it displays the expression’s value in the echo area. Use the r command to display the value again later.
While Edebug is active, it catches all errors (if debug-on-error
is
non-nil
) and quits (if debug-on-quit
is non-nil
)
instead of the standard debugger. When this happens, Edebug displays the
last stop point that it knows about. This may be the location of a call
to a function which was not prepared for Edebug debugging, within which
the error actually occurred.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
In order to use Edebug to debug a function, you must first prepare the function. Preparing a function inserts additional code into it which invokes Edebug at the proper places.
Any call to an Edebug-prepared function activates Edebug. This may or
may not stop execution, depending on the Edebug execution mode in use.
Some Edebug modes only update the display to indicate the progress of
the evaluation without stopping execution. The default initial Edebug
mode is step
which does stop execution. See section Edebug Modes.
Once you have loaded Edebug, the command C-M-x is redefined so
that when used on a function or macro definition, it prepares the
function or macro if given a prefix argument. If the variable
edebug-all-defuns
is non-nil
, that inverts the meaning of
the prefix argument: then C-M-x prepares the function or macro
unless it has a prefix argument. The default value of
edebug-all-defuns
is nil
. The command M-x
edebug-all-defuns toggles the value of the variable
edebug-all-defuns
.
If edebug-all-defuns
is non-nil
, then the commands
eval-region
and eval-current-buffer
also prepare any
functions and macros whose definitions they evaluate.
Loading a file does not prepare functions and macros for Edebug.
See @ref{Evaluation} for discussion of other evaluation functions available inside of Edebug.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Edebug supports several execution modes for running the program you are debugging. We call these alternatives Edebug modes; do not confuse them with major modes or minor modes. The current Edebug mode determines how Edebug displays the progress of the evaluation, whether it stops at each stop point, or continues to the next breakpoint, for example.
Normally, you specify the Edebug mode for execution by typing a command to continue the program in a certain mode. Here is a table of these commands. All except for S resume execution of the program, at least for a certain distance.
Stop: don’t execute any more of the program for now, just wait for more Edebug commands.
Step: stop at the next stop point encountered.
Trace: pause one second at each Edebug stop point.
Rapid trace: mention each stop point, but don’t actually pause.
Go: run until the next breakpoint. See section Breakpoints.
Continue: pause for one second at each breakpoint, but don’t stop.
Continue: mention each breakpoint, but don’t actually pause.
Non-stop: ignore breakpoints. You can still stop the program by typing S.
In general, the execution modes earlier in the above list run the program more slowly or stop sooner.
When you enter a new Edebug level, the mode comes from the value of the
variable edebug-initial-mode
. By default, this specifies
step mode. If the mode thus specified is not stop mode, then the
Edebug level executes the program (or part of it).
While executing or tracing, you can interrupt the execution by typing any Edebug command. Edebug stops the program at the next stop point and then executes the command that you typed. For example, typing t during execution switches to trace mode at the next stop point.
You can use the S command to stop execution without doing anything else.
If your function happens to read input, a character you hit intending to interrupt execution may be read by the function instead. You can avoid such unintended results by paying attention to when your program wants input.
Keyboard macros containing the commands in this section do not completely work: exiting from Edebug, to resume the program, loses track of the keyboard macro. This is not easy to fix.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Run the program forward over one expression. More precisely, set a temporary breakpoint at the position that C-M-f would reach, then execute in go mode so that the program will stop at breakpoints. See Breakpoints for the details on breakpoints.
With a prefix argument n, the temporary breakpoint is placed n sexps beyond point. If the containing list ends before n more elements, then the place to stop is after the containing expression.
Be careful that the position C-M-f finds is a place that the
program will really get to; this may not be true in a
condition-case
, for example.
This command does forward-sexp
starting at point rather than the
stop point, thus providing more flexibility. If you want to execute one
expression from the current stop point, type w first, to move
point there.
Run the program until the end of the containing sexp. If the containing sexp is the top level defun, run until just before the function returns. If that is where you are now, return from the function and then stop.
This command does not exit the currently executing function unless you are positioned after the last sexp of the function.
If the program does a non-local exit, it may fail to reach the temporary breakpoint that this command sets.
Step into the function about to be called. Use this command before any of the arguments of the function call are evaluated, since otherwise it is too late.
One undesirable side effect of using edebug-step-in
is that the
next time the stepped-into function is called, Edebug will be called
there as well.
Proceed to the stop point near where point is. This uses a temporary breakpoint.
The f command runs the program forward over one expression. More precisely, set a temporary breakpoint at the position that C-M-f would reach, then execute in go mode so that the program will stop at breakpoints. See Breakpoints for the details on breakpoints.
With a prefix argument n, the temporary breakpoint is placed n sexps beyond point. If the containing list ends before n more elements, then the place to stop is after the containing expression.
Be careful that the position C-M-f finds is a place that the
program will really get to; this may not be true in a
condition-case
, for example.
The f command uses the existing value of point as the basis for setting the breakpoint, because that is more flexible. To execute one expression from the current stop point, type w and then f.
The o command continues “out of” an expression. It places a temporary breakpoints at the end of the containing sexp. If the containing sexp is the top level defun, it continues until just before the function returns. If that is where you are now, it returns from the function and then stops.
This command does not exit the currently executing function unless you are positioned after the last sexp of the function.
The i command steps into the function about to be called. Use this command before any of the arguments of the function call are evaluated, since otherwise it is too late.
One undesirable side effect of using i is that the next time the stepped-into function is called, Edebug will be called there as well.
The h command proceeds to the stop point near where point is, using a temporary breakpoint.
All the commands in this section may fail to work as expected in case of nonlocal exit, because a nonlocal exit can bypass the temporary breakpoint where you expected the program to stop.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Some miscellaneous commands are described here.
Abort one level of Edebug activity.
Return to the top level editor command loop. This exits all recursive editing levels, including all levels of Edebug activity.
Redisplay the result of the previous expression in the echo area.
Display a backtrace, excluding Edebug’s own functions for clarity.
You cannot use debugger commands in the backtrace buffer in Edebug as you would in the standard debugger.
The backtrace buffer is killed automatically when you continue execution.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
While using Edebug, you can specify breakpoints in the program you are testing: points where execution should stop. You can set a breakpoint at any stop point, as defined in Using Edebug—even before a symbol. For setting and unsetting breakpoints, the stop point that is affected is the first one at or after point in the Edebug buffer. Here are the Edebug commands for breakpoints:
Set a breakpoint at the stop point at or after point. If you use a prefix argument, the breakpoint is temporary (it turns off the first time it stops the program).
Unset the breakpoint (if any) at the stop point at or after the current point.
Set a conditional breakpoint which stops the program only if cond
evaluates to a non-nil
value. If you use a prefix argument, the
breakpoint is temporary (it turns off the first time it stops the
program).
Move point to the next breakpoint in the current function definition.
While in Edebug, you can set a breakpoint with b
(edebug-set-breakpoint
) and unset one with u
(edebug-unset-breakpoint
). First you must move point to a
position at or before the desired Edebug stop point, then hit the key to
change the breakpoint. Unsetting a breakpoint that has not been set
does nothing.
Reevaluating the function with edebug-defun
clears all
breakpoints in the function.
A conditional breakpoint tests a condition each time the program gets there, to decide whether to stop. To set a conditional breakpoint, use x, and specify the condition expression in the minibuffer.
You can make both conditional and unconditional breakpoints temporary by using a prefix arg to the command to set the breakpoint. After breaking at a temporary breakpoint, it is automatically cleared.
Edebug always stops or pauses at a breakpoint except when the Edebug mode is Go-nonstop. In that mode, it ignores breakpoints entirely.
To find out where your breakpoints are, use the B
(edebug-next-breakpoint
) command, which moves point to the next
breakpoint in the function following point, or to the first breakpoint
if there are no following breakpoints. This command does not continue
execution—it just moves point in the buffer.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
These Edebug commands let you view aspects of the buffer and window status that obtained before entry to Edebug.
View the outside window configuration.
Temporarily display the outside current buffer with point at its outside position.
Switch back to the buffer showing the currently executing function, and move point back to the current stop point.
Forget the saved outside window configuration—so that the current
window configuration will remain unchanged when you next exit Edebug (by
continuing the program). Also toggle the edebug-save-windows
variable.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
While within Edebug, you can evaluate expressions “as if” Edebug were not running. Edebug tries to be invisible to the expression’s evaluation.
Evaluate expression exp in the context outside of Edebug. That is, Edebug tries to avoid altering the effect of exp.
Evaluate expression exp in the context of Edebug itself.
Evaluate the expression in the buffer before point, in the context outside of Edebug.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
You can use the evaluation list buffer, called ‘*edebug*’, to evaluate expressions interactively. You can also set up the evaluation list of expressions to be evaluated automatically each time Edebug is reentered.
Switch to the evaluation list buffer ‘*edebug*’.
In the ‘*edebug*’ buffer you can use the commands of Lisp Interaction as well as these special commands:
Evaluate the expression before point, in the context outside of Edebug, and insert the value in the buffer.
Evaluate the expression before point, in the context outside of Edebug.
Build a new evaluation list from the first expression of each group, reevaluate and redisplay. Groups are separated by a line starting with a comment.
Delete the evaluation list group that point is in.
Switch back to the Edebug buffer at the current stop point.
You can evaluate expressions in the evaluation list window with LFD or C-x C-e, just as you would in ‘*scratch*’; but they are evaluated in the context outside of Edebug.
The expressions you enter interactively (and their results) are lost
when you continue execution of your function unless you add them to the
evaluation list with C-c C-u (edebug-update-eval-list
).
This command builds a new list from the first expression of each
evaluation list group. Groups are separated by a line starting
with a comment.
When the evaluation list is redisplayed, each expression is displayed followed by the result of evaluating it, and a comment line. If an error occurs during an evaluation, the error message is displayed in a string as if it were the result. Therefore expressions that use variables not currently valid do not interrupt your debugging.
Here is an example of what the evaluation list window looks like after several expressions have been added to it:
(current-buffer) #<buffer *scratch*> ;--------------------------------------------------------------- (point-min) 1 ;--------------------------------------------------------------- (point-max) 2 ;--------------------------------------------------------------- edebug-outside-point-max "Symbol's value as variable is void: edebug-outside-point-max" ;--------------------------------------------------------------- (recursion-depth) 0 ;--------------------------------------------------------------- this-command eval-last-sexp ;---------------------------------------------------------------
To delete a group, move point into it and type C-c C-d
(edebug-delete-eval-item
), or simply delete the text for it and
update the evaluation list with C-c C-u. When you add a new
group, be sure to add a comment at the beginning.
After selecting ‘*edebug*’, you can return to the source code
buffer (the Edebug buffer) with C-c C-w. The *edebug*
buffer is killed when you continue execution of your function, and
recreated next time it is needed.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
If the results of your expressions contain circular references to other parts of the same structure, you can print them more usefully with the ‘custom-print’.
To load the package and activate custom printing only for Edebug, simply
use the command edebug-install-custom-print-funcs
. Then set the
variable print-circle
to enable special handling of circular
structure. To restore the standard print functions, use
edebug-reset-print-funcs
.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Edebug tries to be transparent to the program you are debugging, but it does not succeed completely. In addition, most evaluations you do within Edebug (see @ref{Evaluation}) occur in the same outside context which is temporarily restored for the evaluation. This section explains precisely how use Edebug fails to be completely transparent.
1.4.11.1 Just Checking | ||
1.4.11.2 Outside Window Configuration | ||
1.4.11.3 Recursive Edit | ||
1.4.11.4 Side Effects |
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Whenever Edebug is entered just to think about whether to take some action, it needs to save and restore certain data.
max-lisp-eval-depth
and max-specpdl-size
are both
incremented for each edebug-enter
call so that your code should
not be impacted by Edebug frames on the stack.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
When Edebug needs to display something (e.g., in trace mode), it saves the current window configuration from “outside” Edebug (@pxref{Window Configurations}). When you exit Edebug (by continuing the program), it restores the previous window configuration.
Emacs redisplays only when it pauses. Usually, when you continue Edebug, the program comes back into Edebug at a breakpoint or after stepping, without pausing or reading input in between. In such cases, Emacs never gets a chance to redisplay the “outside” configuration. What you see is the window configuration for within Edebug, with no interruption.
The window configuration proper does not include which buffer is current or where point and mark are in the current buffer, but Edebug saves and restores these also.
Entry to Edebug for displaying something also saves and restores the following data. (Some of these variables are deliberately not restored if an error or quit signal occurs.)
edebug-save-windows
is non-nil
.
edebug-save-displayed-buffer-points
is non-nil
.
overlay-arrow-position
and
overlay-arrow-string
are saved and restored. This permits
recursive use of Edebug, and use of Edebug while using GUD.
cursor-in-echo-area
is locally bound to nil
so that
the cursor shows up in the window.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
When Edebug is entered and actually reads commands from the user, it saves (and later restores) these additional data:
last-command
, this-command
, last-command-char
, and
last-input-char
. Commands used within Edebug do not affect these
variables outside of Edebug.
But note that it is not possible to preserve the status reported by
(this-command-keys)
and the variable unread-command-char
.
standard-output
and standard-input
.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Edebug operation unavoidably alters some data in Emacs, and this can interfere with debugging certain programs.
max-lisp-eval-depth
and max-specpdl-size
, are also
increased proportionally.
this-command-keys
is changed by
executing commands within Edebug and there appears to be no way to reset
the key sequence from Lisp.
unread-command-char
or unread-command-events
. Entering Edebug while these variables
have nontrivial values can interfere with execution of the program you
are debugging.
command-history
. In rare cases this can alter execution.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
When Edebug prepares for stepping through an expression that uses a Lisp
macro, it needs additional advice to do the job properly. This is
because there is no way to tell which parts of the macro call are forms
to be evaluated. You must explain the format of calls to each macro to
enable Edebug to handle it. To do this, use def-edebug-form-spec
to define the format of calls to a given macro.
Specify which parts of a call to macro macro are subexpressions to be evaluated. The second argument, argpattern, details what the argument list looks like.
Here is a table of the possibilities for argpattern and its subexpressions:
t
A list of any number of evaluated arguments.
0
A list of unevaluated arguments.
sexp
A single unevaluated object.
form
A single evaluated expression.
symbolp
An unevaluated symbol.
integerp
An unevaluated number.
stringp
An unevaluated string.
vectorp
An unevaluated vector.
atom
An unevaluated object that is not a cons cell.
function
A function argument: a quoted symbol, a quoted lambda expression, or a form (that should evaluate to a function or lambda expression). Edebug treats the body of a lambda expression treated as evaluated.
function
A function serves as a predicate—it designates the set of possible
arguments for which it would return non-nil
.
'object
The precise object object, treated as unevaluated.
(patterns)
A list whose elements are described by patterns. A sublist of the same format as the top level, processed recursively.
[patterns]
A sequence of arguments that are described by patterns.
&optional
This symbol serves as a flag saying that all following elements in the
specification list at this level are optional. They may or may not
match arguments; as soon as one does not match, processing of the
specification list at this level terminates. To make just one item
optional, use [&optional pattern]
.
&rest
This symbol serves as a flag saying that the following elements in the
specification list at this level may be repeated, in order, zero or more
times. Only one &rest
may appear at the same level of a
specification list, and &rest
must not be followed by
&optional
.
To specify repetition of certain types of arguments, followed by
dissimilar arguments, use [&rest patterns…]
.
&or
This symbol serves as an operator saying that the following elements in
the specification list at this level are alternatives. To group two or
more list elements as one alternative, bracket them in
[…]
. Only one &or
may appear in a list, and it may
not be followed by &optional
or &rest
. One of the
alternatives must match, unless the &or
is preceded by
&optional
or &rest
.
If the actual arguments of a macro call fail to match the specification, taking account of alternatives, optional arguments and repeated arguments, Edebug reports a syntax error in use of the macro.
The combination of backtracking, &optional
, &rest
,
&or
, and […]
for grouping provides the equivalent of
regular expressions. The (…)
lists require balanced
parentheses, which is the only context free (finite state with stack)
construct supported.
Here are some examples of using def-edebug-form-spec
. First, for
the let
special form:
(def-edebug-form-spec let '((&rest &or symbolp (symbolp &optional form)) &rest form))
Here’s the spec for the for
loop macro (@pxref{Problems with
Macros}) and for the case
and do
macros in ‘cl.el’:
(def-edebug-form-spec for '(symbolp 'from form 'to form 'do &rest form)) (def-edebug-form-spec case '(form &rest (sexp form))) (def-edebug-form-spec do '((&rest &or symbolp (symbolp &optional form form)) (form &rest form) &rest body))
Finally, the functions mapcar
, mapconcat
, mapatoms
,
apply
, and funcall
all take function arguments, and Edebug
defines specifications for them. Here’s one example:
(def-edebug-form-spec apply '(function &rest form))
The backquote (`) macro results in an expression that is not necessarily evaluated. Edebug cannot step through code generated by use of backquote.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
These options affect the behavior of Edebug:
If non-nil
, normal evaluation of defun
and defmacro
forms prepares the functions and macros for stepping with Edebug. This
applies to eval-defun
, eval-region
and
eval-current-buffer
.
The default value is nil
.
If non-nil
, Edebug places stop points before symbols as well as
after.
This option takes effect for a function when you prepare it for stepping with Edebug. Changing the option’s value during execution of Edebug has no effect on the functions already set up for Edebug execution.
If non-nil
, save and restore window configuration on Edebug calls.
It takes some time to save and restore, so if your program does not care
what happens to the window configurations, it is better to set this
variable to nil
.
The default value is t
.
If non-nil
, Edebug saves and restores point and the mark in
source code buffers. The default value is t
.
If non-nil
, save and restore point in all buffers when entering
Edebug mode.
Saving and restoring point in other buffers is necessary if you are debugging code that changes the point of a buffer which is displayed in a non-selected window. If Edebug or the user then selects the window, the buffer’s point will be changed to the window’s point.
Saving and restoring is an expensive operation since it visits each window and each displayed buffer twice for each Edebug call, so it is best to avoid it if you can.
The default value is nil
.
If this variable is non-nil
, it specifies an Edebug mode to start
in each time the program enters a new Edebug recursive-edit level.
Possible values are step
, go
, Go-nonstop
,
trace
, Trace-fast
, continue
, and
Continue-fast
.
The default value is step
.
Non-nil
means display a trace of function entry and exit.
Tracing output is displayed in a buffer named ‘*edebug-trace*’, one
function entry or exit per line, indented by the recursion level. You
can customize this display by replacing the functions
edebug-print-trace-entry
and edebug-print-trace-exit
.
The default value is nil
.
[Top] | [Contents] | [Index] | [ ? ] |
This document was generated on January 16, 2023 using texi2html 5.0.
The buttons in the navigation panels have the following meaning:
Button | Name | Go to | From 1.2.3 go to |
---|---|---|---|
[ << ] | FastBack | Beginning of this chapter or previous chapter | 1 |
[ < ] | Back | Previous section in reading order | 1.2.2 |
[ Up ] | Up | Up section | 1.2 |
[ > ] | Forward | Next section in reading order | 1.2.4 |
[ >> ] | FastForward | Next chapter | 2 |
[Top] | Top | Cover (top) of document | |
[Contents] | Contents | Table of contents | |
[Index] | Index | Index | |
[ ? ] | About | About (help) |
where the Example assumes that the current position is at Subsubsection One-Two-Three of a document of the following structure:
This document was generated on January 16, 2023 using texi2html 5.0.